package com.jr9z.jrjz.service.impl;

import com.jr9z.jrjz.dao.ClassicCaseMapper;
import com.jr9z.jrjz.entity.ClassicCase;
import com.jr9z.jrjz.service.ClassicCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:Mrzhang
 * @Description:
 * @Date:2019.03.25 21:54
 **/
@Service
public class ClassicCaseServiceImpl implements ClassicCaseService {

    @Autowired
    private ClassicCaseMapper classicCaseMapper;

    @Override
    public int deleteByPrimaryKey(Integer classicId) {
        return classicCaseMapper.deleteByPrimaryKey(classicId);
    }

    @Override
    public int insert(ClassicCase record) {
        return classicCaseMapper.insert(record);
    }

    @Override
    public int insertSelective(ClassicCase record) {

        //获取当前最大的sort值

        Integer sort = classicCaseMapper.selectMaxSort();
        //如果为0，目前还没有数据
        if (sort == 0) {
            //将sort值设为1
            record.setSort(1);
        } else {
            //否则在最大的sort值上加一
            record.setSort(sort + 1);
        }
        System.out.println(record.getDetail());
        return classicCaseMapper.insertSelective(record);
    }

    @Override
    public ClassicCase selectByPrimaryKey(Integer classicId) {
        return classicCaseMapper.selectByPrimaryKey(classicId);
    }

    @Override
    public int updateByPrimaryKeySelective(ClassicCase record) {
        return classicCaseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(ClassicCase record) {
        return classicCaseMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public int updateByPrimaryKey(ClassicCase record) {
        return classicCaseMapper.updateByPrimaryKey(record);
    }

    @Override
    public void moveUp(Integer classicId) {

        //获取要上移的那条数据的信息
        ClassicCase classicCase = classicCaseMapper.selectByPrimaryKey(classicId);

        //查询上一条记录
        ClassicCase casePrev = classicCaseMapper.moveUp(classicCase.getSort());

        //最上面的记录不能上移
        if (casePrev == null) {
            return;
        }
        //交换两条记录的sort值
        Integer temp = classicCase.getSort();
        classicCase.setSort(casePrev.getSort());
        casePrev.setSort(temp);

        //更新到数据库
        classicCaseMapper.updateByPrimaryKeySelective(classicCase);
        classicCaseMapper.updateByPrimaryKeySelective(casePrev);
    }


    @Override
    public void moveDown(Integer classicId) {
        //获取要下移的那条数据的信息
        ClassicCase classicCase = classicCaseMapper.selectByPrimaryKey(classicId);

        //查询下一条记录
        ClassicCase caseNext = classicCaseMapper.moveDown(classicCase.getSort());

        //最下面的记录不能下移
        if (caseNext == null) {
            return;
        }
        //交换两条记录的sort值
        Integer temp = classicCase.getSort();
        classicCase.setSort(caseNext.getSort());
        caseNext.setSort(temp);

        //更新到数据库
        classicCaseMapper.updateByPrimaryKeySelective(classicCase);
        classicCaseMapper.updateByPrimaryKeySelective(caseNext);
    }

    @Override
    public List<ClassicCase> getClassicList() {
        return classicCaseMapper.getClassicList();
    }

    @Override
    public Long getClassicCount() {
        return classicCaseMapper.getClassicCount();
    }
}
